package com.atao.baokede.exception;

import com.atao.baokede.pojo.Result;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//全局异常处理--Controller层处理
@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        e.printStackTrace();
        if (e.getMessage().contains("register.username")||e.getMessage().contains("login.username")) {
            return Result.error("用户名长度必须在1-16位之间");
        } else if (e.getMessage().contains("register.password")||e.getMessage().contains("login.password")) {
            return Result.error("密码长度必须在5-16位之间");
        } else if(e.getMessage().contains("Maximum upload size exceeded")){
            return Result.error("上传文件大小必须在1MB之内");
        }else {
            return Result.error(StringUtils.hasLength(e.getMessage()) ? e.getMessage() : "操作失败");
        }
    }
}
